<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\System\Info;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class ModifyService
{

    use Service;

    /**
     * 个人设置保存
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function ModifyStoreService( $request )
    {
        $error          = $this->getErrorCode();
        $adminValidator = $this->getAdminValidator();
        switch ( $request->input('status') ) {
            case 1:
                request()->offsetSet('id', Auth::id());
                $validator = $adminValidator->ModifyStoreRenameValidator($request);
                if ( $validator->first() ) return back()->with('error', $validator->first());
                $email = User::ValueModel([ 'id' => $request->input('id') ], 'email');
                if ( $email != $request->input('email') && $request->input('email') != '' ) {
                    $json = Cookie::get('username_' . $request->input('submit'));//获取Cookie信息
                    if ( ! $json ) return back()->with('error', $error::message(1030));
                    $code = explode(',', $json);
                    if ( $request->input('code') != $code[ 0 ] ) return back()->with('error', $error::message(1021));
                    if ( $request->input('email') != $code[ 1 ] || Auth::id() != $code[ 2 ] ) return back()->with('error', $error::message(1022));
                }
                DB::transaction(function () use ( $request ) {
                    $data[ 'email' ]       = $request->input('email');
                    $data1[ 'rename' ]     = $request->input('rename');
                    $data1[ 'content' ]    = $request->input('content');
                    $data1[ 'editor' ]     = $request->input('editor');
                    $data1[ 'updated_at' ] = Carbon::now();//当前时间
                    User::UpdateModel([ 'id' => $request->input('id') ], $data);
                    Info::UpdateModel($request->input('id'), $data1);
                });
                break;
            case 2:
                if ( ! Hash::check($request->input('old_password'), Auth::user()->getAuthPassword()) ) return back()->with('error', $error::message(1024));

                $validator = $adminValidator->ModifyStorePasswordValidator($request);
                if ( $validator->first() ) return back()->with('error', $validator->first());
                $data[ 'password' ]   = bcrypt($request->input('password'));//加密
                $data[ 'updated_at' ] = Carbon::now();//获取时间
                User::UpdateModel([ 'id' => Auth::id() ], $data);
                break;
            default:
                return back()->with('error', $error::message(1001));
                break;
        }
        return back()->with('success', $error::message(203));
    }

}
